/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.*;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import pojos.TimeLog;
/**
 *
 * @author A7med Kotb
 */
public class TimeSheetExcelFile {
     public String generateExcelFile(String fileName ,List<TimeLog> checkedTimeLogs) {
        try {
            File generatedFile=new File(fileName);
            HSSFWorkbook hwb = new HSSFWorkbook();
            HSSFSheet sheet = hwb.createSheet("New Sheet");
            
            HSSFRow rowhead = sheet.createRow((short) 0);
            rowhead.createCell((short) 0).setCellValue("Time_Log_ID");
            rowhead.createCell((short) 1).setCellValue("Project Name");
            rowhead.createCell((short) 2).setCellValue("Task");
            rowhead.createCell((short) 3).setCellValue("Activity");
            rowhead.createCell((short) 4).setCellValue("Work Hours");
            rowhead.createCell((short) 5).setCellValue("Date");
            rowhead.createCell((short) 6).setCellValue("Employee");
            rowhead.createCell((short) 7).setCellValue("Created_At");
            rowhead.createCell((short) 8).setCellValue("Created_By");
            rowhead.createCell((short) 9).setCellValue("Status");
            
            for (int i= 0 ; i<checkedTimeLogs.size() ; i++) {
                HSSFRow row = sheet.createRow((short) i+1);
                row.createCell((short) 0).setCellValue(checkedTimeLogs.get(i).getTlId());
                row.createCell((short) 1).setCellValue(checkedTimeLogs.get(i).getProject().getProjectName());
                row.createCell((short) 2).setCellValue(checkedTimeLogs.get(i).getTask());
                row.createCell((short) 3).setCellValue(checkedTimeLogs.get(i).getActivity());
                row.createCell((short) 4).setCellValue(checkedTimeLogs.get(i).getWorkedHours());
                row.createCell((short) 5).setCellValue(checkedTimeLogs.get(i).getDate());
                row.createCell((short) 6).setCellValue(checkedTimeLogs.get(i).getEmplyee().getBusinessEmail());
                row.createCell((short) 7).setCellValue(checkedTimeLogs.get(i).getCreatedAt());
                row.createCell((short) 8).setCellValue(checkedTimeLogs.get(i).getCreatedBy().getBusinessEmail());
                row.createCell((short) 9).setCellValue(checkedTimeLogs.get(i).getStatus());
            }
            FileOutputStream fileOut = new FileOutputStream(generatedFile);
            hwb.write(fileOut);
            fileOut.close();
            return generatedFile.getAbsolutePath();
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
